package com.example.zp.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import com.example.zp.greenfruit.R;

public class Classify_fragment2 extends Fragment {

    LinearLayout classify_lay;
    ScrollView sll;
    RadioGroup rg_menu;
    ViewPager vp;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.foot_fragment2,null);
        classify_lay = view.findViewById(R.id.classify_lay);//找顶部搜索
        sll = view.findViewById(R.id.sll);//找垂直滚动控件
        rg_menu = view.findViewById(R.id.rg_menu);//找按钮

        RadioButton rb = (RadioButton) rg_menu.getChildAt(0);//通过单选按钮组找到第一个单选按钮
        rb.setChecked(true);

        vp = view.findViewById(R.id.classify_vp);  //找分页试图控件
        initpager();

        //给Viewpager设置监听事件
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override//按钮选择
            public void onPageSelected(int i) {
                RadioButton rb = (RadioButton) rg_menu.getChildAt(i);//通过单选按钮组找到第一个单选按钮
                rb.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //给RadioButton设置监听事件
        rg_menu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int id) {
                int currentWidth=0;//保存宽度
                for (int i=0; i<rg_menu.getWidth(); i++){
                    RadioButton rb = (RadioButton) rg_menu.getChildAt(i);
                    if (i != 0){
                        currentWidth +=rb.getWidth();
                    } //TODO:需要有优化
                    if (rb.getId() == id){
                        currentWidth =currentWidth-(sll.getWidth()-rb.getWidth())/2;
                        vp.setCurrentItem(i);//根据索引显示
                        break;
                    }
                }
                //设置水平滚动轴的位置
                sll.scrollTo(currentWidth,0);

            }
        });


        return view;
    }

    private void initpager() {
        vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                Fragment fragment = null;
                switch (i){
                    case 0:
                        fragment = new Classify_frag1();
                        break;
                    case 1:
                        fragment = new Classify_frag2();
                        break;
                    case 2:
                        fragment = new Classify_frag3();
                        break;
                    case 3:
                        fragment = new Classify_frag4();
                        break;
                    case 4:
                        fragment = new Classify_frag5();
                        break;
                    case 5:
                        fragment = new Classify_frag6();
                        break;
                    case 6:
                        fragment = new Classify_frag7();
                        break;
                    default:
                        break;
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return 7;
            }
        });

    }
}